15. This Pointer
This Pointer
When working with classes it is often helpful to be able to refer to the current class instance or object. For example, given the following
Car
class from a previous lesson, the
IncrementDistance()
method implicitly refers to the current
Car
instance's
distance
attribute:
// The Car class
class Car {
public:
// Method to print data.
void PrintCarData() {
cout << "The distance that the " << color << " car " << number << " has traveled is: " << distance << "\n";
}
// Method to increment the distance travelled.
void IncrementDistance() {
distance++;
}
// Class/object attributes
string color;
int distance = 0;
int number;
};
It is possible to make this explicit in C++ by using the
this
pointer, which points to the current class instance. Using
this
can sometimes be helpful to add clarity to more complicated code:
// The Car class
class Car {
public:
// Method to print data.
void PrintCarData() {
cout << "The distance that the " << this->color << " car " << this->number << " has traveled is: " << this->distance << "\n";
}
// Method to increment the distance travelled.
void IncrementDistance() {
this->distance++;
}
// Class/object attributes
string color;
int distance = 0;
int number;
};
Note: you may see
this
used in some code in the remainder of the course.